﻿using System;
using System.Threading;
using BloggerAutoPosterService.Engine.Net;
using BloggerAutoPosterService.Engine.Utils;
using ThreadTimer = System.Threading.Timer;

namespace BloggerAutoPosterService.Engine.Core
{
    public class ScheduledJobRunner
    {
        #region Fields (4)

        private readonly IRedirectFinder _redirectFinder;
        private readonly IServiceConfig _serviceConfig;
        private ThreadTimer _tmrThreadingTimerDailyCompileSendEmail;
        private ThreadTimer _tmrThreadingTimerReadSitesData;

        #endregion Fields

        #region Constructors (1)

        public ScheduledJobRunner(IRedirectFinder redirectFinder, IServiceConfig serviceConfig)
        {
            _serviceConfig = serviceConfig;
            _redirectFinder = redirectFinder;
        }

        #endregion Constructors

        #region Methods (5)

        // Public Methods (2) 

        public void OnStart()
        {
            try
            {
                ProxySettings.SetDefaultProxy(_serviceConfig);
                createThreadTimer();

                if (_tmrThreadingTimerReadSitesData != null)
                    _tmrThreadingTimerReadSitesData.Change(1 * 60 * 1000, _serviceConfig.ReadSitesDataIntervalMin * 60 * 1000);

                if (_tmrThreadingTimerDailyCompileSendEmail != null)
                    _tmrThreadingTimerDailyCompileSendEmail.Change(1 * 60 * 1000, 1 * 60 * 1000);
            }
            catch (Exception ex)
            {
                new Logger(_serviceConfig).WriteLine(ex);
            }
        }

        public void OnStop()
        {
            try
            {
                if (_tmrThreadingTimerReadSitesData != null)
                    _tmrThreadingTimerReadSitesData.Change(Timeout.Infinite, Timeout.Infinite);
                if (_tmrThreadingTimerDailyCompileSendEmail != null)
                    _tmrThreadingTimerDailyCompileSendEmail.Change(Timeout.Infinite, Timeout.Infinite);
            }
            catch (Exception ex)
            {
                new Logger(_serviceConfig).WriteLine(ex);
            }
        }
        // Private Methods (3) 

        void createThreadTimer()
        {
            _tmrThreadingTimerReadSitesData =
                new ThreadTimer(tmrThreadingTimerReadSitesDataTimerCallback,
                                null,
                                Timeout.Infinite,
                                1000);

            _tmrThreadingTimerDailyCompileSendEmail =
                new ThreadTimer(tmrThreadingTimerCompileSendEmailTimerCallback,
                                null,
                                Timeout.Infinite,
                                1000);
        }

        private void tmrThreadingTimerCompileSendEmailTimerCallback(object state)
        {
            try
            {
                var now = DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00");
                if (now == _serviceConfig.PostAt)
                {
                    new RunAll(_serviceConfig, _redirectFinder).PostDataMarkAsSent();
                }
            }
            catch (Exception ex)
            {
                new Logger(_serviceConfig).WriteLine(ex);
            }
        }

        private void tmrThreadingTimerReadSitesDataTimerCallback(object state)
        {
            try
            {
                new RunAll(_serviceConfig, _redirectFinder).ReadFeedSaveData();
                Memory.ReEvaluatedWorkingSet();
            }
            catch (Exception ex)
            {
                new Logger(_serviceConfig).WriteLine(ex);
            }
        }

        #endregion Methods
    }
}
